package org.icress.fusion.admin

import org.icress.fusion.shiro.User
import org.apache.shiro.SecurityUtils
import org.icress.fusion.b2b.Department
import org.hibernate.FetchMode as FM

class WelcomeService {

    static transactional = true

    def getDepartmentName = {
      def currentUser = User.findByUsername( SecurityUtils.getSubject().getPrincipal() )
      def department = Department.get((currentUser?.department?.id) ?: -1L)
      return (department?.name) ?: ''
    }

    def getGroups = {
      def currentUser = User.findByUsername( SecurityUtils.getSubject().getPrincipal() )
		  //get user group
		  def groups = Group.withCriteria{
        createAlias 'users', 'u'
			  projections{
          groupProperty('name')
			  }
			  eq("isActive", true)
			  eq("u.id", currentUser.id)
			  fetchMode "users", FM.JOIN
		  }
      return groups
    }
}
